package com.luyuan.crm.member.web;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.faces.model.DataModel;

import org.hibernate.criterion.Order;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.luyuan.crm.member.model.CallBackRecord;
import com.luyuan.crm.member.model.Member;
import com.luyuan.crm.member.service.CallBackRecordService;
import com.luyuan.crm.member.service.MemberService;
import com.luyuan.crm.user.service.PartyService;
import com.ruijie.components.security.model.AppUser;
import com.ruijie.web.jsf.action.BaseAction;
import com.ruijie.web.jsf.context.TableContext;
import com.ruijie.web.jsf.model.PageDataModel;
import com.ruijie.web.jsf.model.TableOrder;
import com.ruijie.web.jsf.model.TablePage;
import com.ruijie.foundation.query.Page;
import com.ruijie.foundation.query.criteria.CriteriaTemplate;
import com.ruijie.foundation.query.criteria.RestrictionTemplate;
import com.ruijie.foundation.security.AppUserContext;

@Component("callbackRecordAction")
@Scope("view")
public class CallbackRecordAction extends BaseAction implements InitializingBean {

    // private final static Log logger = LogFactory.getLog(CallbackRecordAction.class);

    @Resource(name = "callBackRecordService")
    private CallBackRecordService callBackRecordService;

    @Resource(name = "memberService")
    private MemberService memberService;

    @Resource(name = "partyService")
    private PartyService partyService;

    private CallBackRecord criteria = new CallBackRecord();

    String partyId;

    // 添加或编辑模型
    private CallBackRecord callBackRecord = new CallBackRecord();

    private String memberId;

    public CallBackRecord getCallBackRecord() {
        return callBackRecord;
    }

    public CallBackRecord getCriteria() {
        return criteria;
    }

    public DataModel getTableModel() {
        /**
         * TableLimit.get(FacesContext）只能RenderPhase阶段被调用，否则会返回Null
         */
        CriteriaTemplate c = CriteriaTemplate.forClass(CallBackRecord.class);
        RestrictionTemplate r = new RestrictionTemplate(true);
        Member member = memberService.getById(memberId);
        c.add(r.eq("member", member));
        TablePage tablePage = TableContext.getTablePage();
        if (tablePage == null) {
            return new PageDataModel(new Page());
        }
        List<TableOrder> tableOrders = TableContext.getTableOrders();
        List<Order> orders = new ArrayList<Order>();
        for (TableOrder tableOrder : tableOrders) {
            Order order = tableOrder.toOrder();
            if (order != null) {
                orders.add(order);
            }
        }
        Page page = callBackRecordService.pagedQuery(c, tablePage.getPage(), tablePage.getPageSize(), orders);
        return new PageDataModel(page);
    }

    public void setCallBackRecordService(CallBackRecordService callBackRecordService) {
        this.callBackRecordService = callBackRecordService;
    }

    public void toAddpage(String memberRequesetId) {
        setRequestAttribute("member", memberRequesetId, true);
    }

    public void add() {
        // memberId = this.getRequestParameter("memberId");
        // String memberRequesetId = (String) getRequestAttribute("member");
        callBackRecord.setRecordTime(new Date());
        Member member = memberService.getById(memberId);
        callBackRecord.setMember(member);
        // Party party = partyService.getById(partyId);
        callBackRecord.setParty(partyService.getByAppUser((AppUser) AppUserContext.getCurrentUser()));
        callBackRecordService.addCallBackRecord(callBackRecord);
        addGlobalI18nMessage("crud_add_success_msg");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        // memberId = (String) getRequestAttribute("member");
        if (getRequestParameter("memberId") != null) {
            memberId = getRequestParameter("memberId");
        }
    }
}
